<?php
namespace Crm\Api;

use PhalApi\Api;
use PhalApi\Request;
use PhalApi\Exception\BadRequestException;
use Auth\Api\Token as Token;
use Crm\Domain\Project as DomainProject;


/**
 * 项目服务类
 *
 * @author: luodong <751450467@qq.com> 2020-01-04
 */

class Project extends Api {

    /**
     * token对象
     */
    public $token;

    /**
     * 当前用户信息
     */
    public $user;

    public function __construct()
    {
        //允许所有域名发起的跨域请求
        header('Access-Control-Allow-Origin: *');  //支持全域名访问，不安全，部署后需要固定限制为客户端网址
        //header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); //支持的http 动作
        //header('Access-Control-Allow-Headers:Authorization,content-type');  //响应头 请按照自己需求添加。
        //$this->di = \PhalApi\DI();
        $this->token = new Token();

    }

    public function getRules() {
        return array(
            //项目接口
            'getList' => array(),
            'bulidList' => array(),
            'hotList' => array(
                'mall_id' => array('name' => 'mall_id', 'type'=>'int','require'=>false,'desc'=>'默认商城ID'),
                'city_code'=>array('name' => 'city_code', 'type'=>'int', 'require' => false, 'desc' => '城市编码'),
                'digest'=>array('name' => 'digest', 'type'=>'int', 'require' => false, 'desc' => '热门推荐')
            ),
            'detail' => array(
                'id'     => array('name' => 'id', 'type'=>'int', 'require' => true, 'desc' => '项目ID'),
                'pm_type'=> array('name' => 'pm_type', 'type'=>'int', 'require' => false, 'desc' => '物业类型'),
            ),
            'huxing' => array(
                'id'     => array('name' => 'id', 'type'=>'int', 'require' => true, 'desc' => '项目ID')
            ),
            'saler' => array(
                'id'     => array('name' => 'id', 'type'=>'int', 'require' => true, 'desc' => '项目ID')
            ),
            'gallery' => array(
                'id'     => array('name' => 'id', 'type'=>'int', 'require' => true, 'desc' => '项目ID'),
                'type'   => array('name' => 'type', 'type'=>'string', 'require' => true, 'desc' => '图片类型')
            ),
            'circum' => array(
                'id'     => array('name' => 'id', 'type'=>'int', 'require' => true, 'desc' => '项目ID')
            ),
            'license' => array(
                'id'     => array('name' => 'id', 'type'=>'int', 'require' => true, 'desc' => '项目ID')
            )
        );

    }

    /**
     * 获取搜索参数
     * @desc 获取搜索栏参数定义
     * @return obj 参数信息
     */
    public function getParams(){
        $domain = new DomainProject();
        $params['warn_outdays'] = $domain->baseCode->customer['warn_outdays'];
        return $params;
    }

    /**
     * 返回项目列表
     * @desc 项目列表
     * @return obj 项目信息
     */
    public function getList() {
        //$this->user = $this->token->checkToken();

        $params = \PhalApi\DI()->request->getAll();
        $domain = new DomainProject();
        $rs = $domain->search($params);
        return $rs;
    }

    /**
     * 返回楼栋列表
     * @desc 项目楼栋列表
     * @return obj 项目楼栋信息
     */
    public function bulidList() {
        //$this->user = $this->token->checkToken();

        $params = \PhalApi\DI()->request->getAll();
        $project_id = $params['project_id'];
        $domain = new DomainProject();
        $rs = $domain->getBuildList($project_id);
        return $rs;
    }

    /**
     * 热门楼盘
     * @desc 获取热门楼盘
     * @return array data
     * @return int time 当前时间戳
     */
    public function hotList(){
        error_log("hotList!", 0);

        $Project = new DomainProject();
        $params['mall_id'] = $this->mall_id;
        $params['digest'] = $this->digest;
        $params['city_code'] = $this->city_code;
        $list = $Project->search($params);
        return $list;
    }

    /**
     * 获取项目详情
     * @return array data 客户信息
     * @return int time 当前时间戳
     */
    public function detail()
    {
        error_log("project detail!", 0);
        $domain = new DomainProject();
        $data = $domain->getProjectInfo($this->id,$this->pm_type);
        return $data;
    }

    /**
     * 项目户型
     * @return array data 户型信息
     * @return int time 当前时间戳
     */
    public function huxing()
    {
        error_log("project huxing!", 0);
        $domain = new DomainProject();
        $data = $domain->getProjectMode($this->id,0,4);
        return $data;
    }

    /**
     * 项目置业顾问
     * @return array data 置业顾问信息
     * @return int time 当前时间戳
     */
    public function saler()
    {
        error_log("project saler!", 0);
        $domain = new DomainProject();
        $data = $domain->getProjectSaler($this->id);
        return $data;
    }


    /**
     * 项目相册
     * @return array data 户型信息
     * @return int time 当前时间戳
     */
    public function gallery()
    {
        error_log("project huxing!", 0);
        $domain = new DomainProject();
        if($this->type=='huxing') {
            $pic = $domain->getProjectMode($this->id);
            //处理图片
            $cPic['1']['title'] = '全部';
            $cPic['1']['total'] = 0;

            $cPic['2']['title'] = '二室';
            $cPic['2']['total'] = 0;

            $cPic['3']['title'] = '三室';
            $cPic['3']['total'] = 0;

            $cPic['4']['title'] = '四室';
            $cPic['4']['total'] = 0;
            foreach ($pic as $item){
                if($item['shi']=='2'){
                    $cPic['2']['total'] = $cPic['2']['total']+1;
                    $cPic['2']['list'][] = $item;

                    $cPic['1']['total'] = $cPic['1']['total']+1;
                    $cPic['1']['list'][] = $item;
                }
                if($item['shi']=='3'){
                    $cPic['3']['total'] = $cPic['3']['total']+1;
                    $cPic['3']['list'][] = $item;

                    $cPic['1']['total'] = $cPic['1']['total']+1;
                    $cPic['1']['list'][] = $item;
                }
                if($item['shi']=='4'){
                    $cPic['4']['total'] = $cPic['4']['total']+1;
                    $cPic['4']['list'][] = $item;

                    $cPic['1']['total'] = $cPic['1']['total']+1;
                    $cPic['1']['list'][] = $item;
                }
            }
        }else{
            $pic = $domain->getProjectPic($this->id);
            //处理图片
            $cPic['1']['title'] = '全部';
            $cPic['1']['total'] = 0;
            $cPic['1']['code'] = 20900;

            $cPic['2']['title'] = '效果图';
            $cPic['2']['total'] = 0;
            $cPic['2']['code'] = 20902;

            $cPic['3']['title'] = '样板间';
            $cPic['3']['total'] = 0;
            $cPic['3']['code'] = 20905;

            $cPic['4']['title'] = '交通图';
            $cPic['4']['total'] = 0;
            $cPic['4']['code'] = 20907;
            foreach ($pic as $item){
                if($item['code']=='20902'){
                    $cPic['2']['total'] = $cPic['2']['total']+1;
                    $cPic['2']['list'][] = $item;

                    $cPic['1']['total'] = $cPic['1']['total']+1;
                    $cPic['1']['list'][] = $item;
                }
                if($item['code']=='20905'){
                    $cPic['3']['total'] = $cPic['3']['total']+1;
                    $cPic['3']['list'][] = $item;

                    $cPic['1']['total'] = $cPic['1']['total']+1;
                    $cPic['1']['list'][] = $item;
                }
                if($item['code']=='20907'){
                    $cPic['4']['total'] = $cPic['4']['total']+1;
                    $cPic['4']['list'][] = $item;

                    $cPic['1']['total'] = $cPic['1']['total']+1;
                    $cPic['1']['list'][] = $item;
                }
            }
        }
        return $cPic;
    }


    /**
     * 项目周边
     * @return array data 户型信息
     * @return int time 当前时间戳
     */
    public function circum()
    {
        error_log("project circum!", 0);
        $domain = new DomainProject();
        $rlt = $domain->getProjectCircum($this->id);
        return $rlt;
    }

    /**
     * 项目预售信息
     * @return array data 项目预售信息
     * @return int time 当前时间戳
     */
    public function license()
    {
        error_log("project license!", 0);
        $domain = new DomainProject();
        $data = $domain->getLicense($this->id,1);
        return $data;
    }


}
